package pure.engine.world 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import pure.engine.core.ns_despair;
	import pure.engine.model.Renderer;
	import pure.engine.core.ns_despair;

	use namespace ns_despair
	
	/**
	 * 碎片
	 * [Author] - PURE
	 * 
	 */
final public class Fragment extends Sprite
{
	public function Fragment()
	{
		m_bitmap = new Bitmap();
		this.addChild(m_bitmap);
	}
	
	

	/**
	 * 初期化
	 */
	//public function initialize():Fragment
	//{
//
		//
		//return this;
	//}
	
	
	/**
	 * 更新全部
	 */
	final ns_despair function updateAll(bitmapData:BitmapData, x:Number, y:Number, smoothing:Boolean):void
	{
		this.m_bitmap.bitmapData = bitmapData;
		this.m_bitmap.x = x;
		this.m_bitmap.y = y;
		if (smoothing)
		{
			this.m_bitmap.smoothing = true;
		}
	}
	
	
	/**
	 * 更新画像
	 */
	final ns_despair function updateImage(bitmapData:BitmapData, smoothing:Boolean):void
	{
		this.m_bitmap.bitmapData = bitmapData;
		if (smoothing)
		{
			this.m_bitmap.smoothing = true;
		}
	}
	
	
	/**
	 * 更新形变
	 */
	final ns_despair function updateTransform(smoothing:Boolean):void
	{
		this.m_bitmap.smoothing = smoothing;
	}
	
	
	/**
	 * 失效
	 */
	final ns_despair function invalidate():void
	{
		m_bitmap.bitmapData = null;
	}
	
	
	/**
	 * 获取滑鼠处像素
	 */
	final ns_despair function getMousePixel():uint
	{
		return Boolean(m_bitmap.bitmapData) ? m_bitmap.bitmapData.getPixel32((m_bitmap.mouseX + 0.5 >> 0), (m_bitmap.mouseY + 0.5 >> 0)) : 0;
	}
	
	
	/**
	 * 回收
	 */
	final ns_despair function recycle(transformDirty:Boolean):void
	{
		this.m_bitmap.bitmapData = null;
		cachedFragmentList[m_numFragment++] = this;
		if (transformDirty)
		{
			this.scaleX = this.scaleY = this.alpha = 1;
			this.rotation = 0;
			this.filters = null;
		}
	}

	
	/**
	 * 获取
	 */
	ns_despair static function getFragment():Fragment
	{
		return (m_numFragment > 0 ? m_numFragment-- : 0) ? cachedFragmentList.pop() : new Fragment();
	}
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair static var cachedFragmentList:Vector.<Fragment> = new Vector.<Fragment>();
	
	ns_despair static var m_numFragment:int;
	
	ns_despair var m_bitmap:Bitmap = new Bitmap(null, 'never');

}
}